package org.esgl3d.rendering;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

class VertexArray extends VertexContainer {
	private final GL10 gl;
	
	public VertexArray(int sizeInBytes, VertexFormat type, GL10 gl) {
		super(sizeInBytes,type);
		
		this.gl = gl;
	}

	@Override
	public void draw(PrimitiveType type, int startVerticeIndex, int numberOfVertices) {
		int glType = GL10.GL_TRIANGLES;
		
		switch (type) {
		case Line:
			glType = GL10.GL_LINES;
			break;
		case Point:
			glType = GL10.GL_POINTS;
			break;
		case TriangleStrip:
			glType = GL10.GL_TRIANGLE_STRIP;
			break;
		case Triangle:
			break;
		}
		
		if (format.components[VertexFormat.COLOR] > 0) {
			gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
			if (structure == BufferStructure.Stacked)
				gl.glColorPointer(format.components[VertexFormat.COLOR], GL10.GL_FLOAT, 0, buffer.position(format.offsets[VertexFormat.COLOR] * numberOfVertices));
			else
				gl.glColorPointer(format.components[VertexFormat.COLOR], GL10.GL_FLOAT, format.totalFloats, buffer.position(format.offsets[VertexFormat.COLOR]));
		} else
			gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
			
		
		if (format.components[VertexFormat.NORMAL] > 0) {
			gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
			if (structure == BufferStructure.Stacked)
				gl.glNormalPointer(GL10.GL_FLOAT, 0, buffer.position(format.offsets[VertexFormat.NORMAL] * numberOfVertices));
			else
				gl.glNormalPointer(GL10.GL_FLOAT, format.totalFloats, buffer.position(format.offsets[VertexFormat.NORMAL]));
		} else
			gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
		
		if (format.components[VertexFormat.TEXTURE] > 0) {
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			if (structure == BufferStructure.Stacked)
				gl.glTexCoordPointer(format.components[VertexFormat.TEXTURE], GL10.GL_FLOAT, 0, buffer.position(format.offsets[VertexFormat.TEXTURE] * numberOfVertices));
			else
				gl.glTexCoordPointer(format.components[VertexFormat.TEXTURE], GL10.GL_FLOAT, format.totalFloats, buffer.position(format.offsets[VertexFormat.TEXTURE]));
		} else
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		if (format.components[VertexFormat.POSITION] > 0) {
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			if (structure == BufferStructure.Stacked)
				gl.glVertexPointer(format.components[VertexFormat.POSITION], GL10.GL_FLOAT, 0, buffer.position(format.offsets[VertexFormat.POSITION] * numberOfVertices));
			else
				gl.glVertexPointer(format.components[VertexFormat.POSITION], GL10.GL_FLOAT, format.totalFloats, buffer.position(format.offsets[VertexFormat.POSITION]));
		} else
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
				
		
		gl.glDrawArrays(glType, startVerticeIndex, numberOfVertices);
	}

	@Override
	public void synchronize() {
		// TODO Auto-generated method stub
		
	}
}
